/*
AlliedBridge Content Management System
Copyright (C) 2006 AlliedBridge

This program is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software Foundation;

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.volume4.alliedbridge.model.eventCalendar;

import org.volume4.utils.ServletUtilities;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: Schalk
 * Date: 2006/04/23
 * Time: 12:33:45
 * To change this template use File | Settings | File Templates.
 */

public class RemoveCalendarItem extends HttpServlet {
    // Should the following three String declarations be private or final?
    // Should they be in an interface?
    public String DRIVER,URL,USER,PASS;
    public String app_root,messageSuccess,messageFailed;
    public String id,event,place,venue,start_date,end_date,start_time,end_time;

    /* The below would need to be set via a properties or XML
     * configuration file per company dependant on which company
     * are accessing these methods.
     *
     * There may be a better way to do this and this should be
     * investigated.
    */
    public void init() throws ServletException {
    ServletContext context = getServletContext();
    DRIVER = context.getInitParameter("driver");
    URL = context.getInitParameter("mc_dburl");
    USER = context.getInitParameter("mc_dbuser");
    PASS = context.getInitParameter("mc_dbpass");
    }

    public void processRequest (HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    Connection con = null;
    messageSuccess = "Calendar item was successfuly removed.";
    messageFailed = "Removal of calendar item failed";

    try {

        Class.forName(DRIVER);
        con = DriverManager.getConnection(URL,USER,PASS);
        Statement stmt = con.createStatement();

        id = request.getParameter("id");

        String query =
                "DELETE FROM ab_calendar " +
                "WHERE id= '" + id + "'";
        // There should be a timer task that runs once every three days to optimize the entire database.
        // The timer task should run at 11:00 PM GMT+2
        // When this is implemented the queries such as the one below can be removed.
        String optimize = "OPTIMIZE TABLE ab_calendar";

                int result = stmt.executeUpdate(query);
                int optimization_result = stmt.executeUpdate(optimize);

                app_root = request.getContextPath();
                response.sendRedirect(app_root + "/application/application_messages/process_result.jsp?message=" + messageSuccess);

} catch (SQLException ex) {
    out.println("\nERROR:------ SQLException -----\n");
        while (ex != null) {
            out.println("Message: " + ex.getMessage() + "<br>");
            out.println("SQLState: " + ex.getSQLState() + "<br>");
            out.println("ErrorCode :" + ex.getErrorCode());
            ex = ex.getNextException();
        }
    } catch (Exception e) {
        e.printStackTrace();
} finally {
    try {
    if (con != null)
        con.close();
    } catch (SQLException ex) {
            out.println("\nERROR:------ SQLException -----\n");
            out.println("Message: " + ex.getMessage());
            out.println("SQLState: " + ex.getSQLState());
            out.println("ErrorCode :" + ex.getErrorCode());
        }
            }
    }

    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

}